<?php
// LJCLI is a command line interface to LiveJournal.com web-serive.
// The XML-RPC interface of www.LiveJournal.com is used.
// See LiveJournal Programming (http://www.livejournal.com/doc/server/ljp.index.html)
// for the further information.

// CLI: php ljcli.php <username> <password> <path_to_file_to_post>

require_once 'LJClient.php';

define(NEWLINE_CHAR_CODE, 0x0A);

$ProgramName = $argv[0];

// check for arguments number
if ($argc < 2) die("Error: wrong number of arguments\nUsage: php ljcli.php <method> [<arg1> [<arg2> [...]]");

$method = $argv[1];

if ($method == "post") {
	$username = $argv[2]; // we don't check user name and password, because LJ
	$password = $argv[3]; // already has a check for them
	$filename = $argv[4];

	if(!file_exists($filename)) die("Error: file " . $filename . " does not exist");

	$file_text = file_get_contents($filename);
	if(trim($file_text) == '') die("Error: file " . $filename . " is empty");

	$post_strs = explode(chr(NEWLINE_CHAR_CODE), $file_text, 2);
	$post_subject = $post_strs[0];
	$post_text = trim($post_strs[1]);

	$ljcli = new LJClient($username, $password);
	$res_str = $ljcli->postEvent($post_subject, $post_text);
	echo $res_str;

//	$events = $ljcli->getEvents();
//	foreach ($events as $e) {
//		echo $e."\n";
//	}
} else if($method == "getevents") {

} else {
	die("method '{$method}' is not implemented!");
}